package cn.com.aiton.common.utils;

import cn.com.aiton.common.constant.Gbt209992007;
import cn.com.aiton.common.model.extend.AitonExtendDetectorStateAndStatesDomain;
import cn.com.aiton.common.model.extend.AitonExtendDetectorStateDomain;
import cn.com.aiton.common.model.extend.AitonExtendDetectorStatesDomain;


import java.util.ArrayList;
import java.util.List;

public class AitonExtendDetectorStatesUtil {
    public static AitonExtendDetectorStateAndStatesDomain byte2DetectorState(byte[] bytes){
        AitonExtendDetectorStateAndStatesDomain aitonExtendDetectorStateAndStatesDomain = new AitonExtendDetectorStateAndStatesDomain();
        List<AitonExtendDetectorStateDomain> aitonExtendDetectorStateDomains = new ArrayList<>();
        List<AitonExtendDetectorStatesDomain> aitonExtendDetectorStatesDomains = new ArrayList<>();
        byte[] detectorArray = new byte[Gbt209992007.DETECTOR_STATE_BYTE_LEN * Gbt209992007.DETECTOR_STATE_BYTE_SIZE];
        System.arraycopy(bytes, 4, detectorArray, 0, Gbt209992007.DETECTOR_STATE_BYTE_LEN * Gbt209992007.DETECTOR_STATE_BYTE_SIZE);
        byte[][] twoArray = ByteUtils.oneArrayToTwoArray(detectorArray, Gbt209992007.DETECTOR_STATE_BYTE_LEN, Gbt209992007.DETECTOR_STATE_BYTE_SIZE);
        AitonExtendDetectorStateDomain ds;
        AitonExtendDetectorStatesDomain     dso;
        for (int i = 0; i < Gbt209992007.DETECTOR_STATE_BYTE_LEN; i++) {
            ds = new AitonExtendDetectorStateDomain();
            dso = new AitonExtendDetectorStatesDomain();
            dso.setDetectorStateId(twoArray[i][0]);
            byte state = twoArray[i][1];
            if (0x01 == (byte) (state & 0x01)) {
                dso.setDetectorState1((byte) 0x01);
            }
            if (0x02 == (byte) (state & 0x02)) {
                dso.setDetectorState2((byte) 0x01);
            }
            if (0x04 == (byte) (state & 0x04)) {
                dso.setDetectorState3((byte) 0x01);
            }
            if (0x08 == (byte) (state & 0x08)) {
                dso.setDetectorState4((byte) 0x01);
            }
            if (0x10 == (byte) (state & 0x10)) {
                dso.setDetectorState5((byte) 0x01);
            }
            if (0x20 == (byte) (state & 0x20)) {
                dso.setDetectorState6((byte) 0x01);
            }
            if (0x40 == (byte) (state & 0x40)) {
                dso.setDetectorState7((byte) 0x01);
            }
            if (0x80 == (byte) (state & 0x80)) {
                dso.setDetectorState8((byte) 0x01);
            }
            byte alert = twoArray[i][2];
            if (0x01 == (byte) (alert & 0x01)) {
                dso.setDetectorStateAlert1((byte) 0x01);
            }
            if (0x02 == (byte) (alert & 0x02)) {
                dso.setDetectorStateAlert2((byte) 0x01);
            }
            if (0x04 == (byte) (alert & 0x04)) {
                dso.setDetectorStateAlert3((byte) 0x01);
            }
            if (0x08 == (byte) (alert & 0x08)) {
                dso.setDetectorStateAlert4((byte) 0x01);
            }
            if (0x10 == (byte) (alert & 0x10)) {
                dso.setDetectorStateAlert5((byte) 0x01);
            }
            if (0x20 == (byte) (alert & 0x20)) {
                dso.setDetectorStateAlert6((byte) 0x01);
            }
            if (0x40 == (byte) (alert & 0x40)) {
                dso.setDetectorStateAlert7((byte) 0x01);
            }
            if (0x80 == (byte) (alert & 0x80)) {
                dso.setDetectorStateAlert8((byte) 0x01);
            }

            ds.setDetectorStateId(twoArray[i][0]);
            ds.setDetectorState(twoArray[i][1]);
            ds.setDetectorStateAlert(twoArray[i][2]);
            aitonExtendDetectorStateDomains.add(ds);
            aitonExtendDetectorStatesDomains.add(dso);
        }
        aitonExtendDetectorStateAndStatesDomain.setAitonExtendDetectorStateDomains(aitonExtendDetectorStateDomains);
        aitonExtendDetectorStateAndStatesDomain.setAitonExtendDetectorStatesDomains(aitonExtendDetectorStatesDomains);
        return aitonExtendDetectorStateAndStatesDomain;
    }
}
